package com.cq.hd.admin.controller;

import com.cq.hd.admin.annotation.RequiresPermissionsDesc;
import com.cq.hd.admin.po.TbBusinessAdminUserPo;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbBusinessRechargeApplyApi;
import com.cq.hd.member.api.dto.BusinessRechargeApplyDto;
import com.cq.hd.member.api.dto.BusinessRechargeApplyPageDto;
import com.cq.hd.member.api.vo.BusinessRechargeApplyPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商家后台补贴充值申请相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/business/rechargeApply")
public class AdminBusinessRechargeApplyController extends BaseController {

    @Autowired
    private TbBusinessRechargeApplyApi businessRechargeApplyApi;

    @ApiOperation(value = "充值申请列表")
//    @RequiresPermissions("adminBusiness:rechargeApply:page")
    @RequiresPermissionsDesc(menu = {"商家后台", "充值申请列表"}, button = "查询")
    @GetMapping("/page")
    public Result<Page<BusinessRechargeApplyPageVo>> page(BusinessRechargeApplyPageDto businessRechargeApplyPageDto) {
        Long businessId = getBusinessId();
        businessRechargeApplyPageDto.setBusinessId(businessId);
        return businessRechargeApplyApi.page(businessRechargeApplyPageDto);
    }

    @ApiOperation(value = "商家充值申请")
//    @RequiresPermissions("adminBusiness:rechargeApply:audit")
    @RequiresPermissionsDesc(menu = {"商家后台", "充值申请"}, button = "审核")
    @PostMapping("/apply")
    public Result<Integer> apply(@RequestBody BusinessRechargeApplyDto businessRechargeApplyDto) {
        Long businessId = getBusinessId();
        businessRechargeApplyDto.setBusinessId(businessId);
        return businessRechargeApplyApi.apply(businessRechargeApplyDto);
    }

    protected Long getBusinessId() {
        TbBusinessAdminUserPo businessAdminUserPo = getBusinessAdminUser();
        if (businessAdminUserPo == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        Long businessId = businessAdminUserPo.getBusinessId();
        Long appUserId = businessAdminUserPo.getAppUserId();
        if (businessId == null || businessId <= 0 || appUserId == null || appUserId <= 0) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return businessId;
    }
}
